/*
 * Copyright (C) 2008 ZXing authors
 * Copyright 2011 Robert Theis
 * Copyright 2012 Hai Do Minh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hpcc.hut.edu.vn.ocr;

import hpcc.hut.edu.vn.ocr.language.LanguageCodeHelper;
import hpcc.hut.edu.vn.ocr.language.TranslateBingAsyncTask;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

/**
 * Class to handle preferences that are saved across sessions of the app. Shows
 * a hierarchy of preferences to the user, organized into sections. These
 * preferences are displayed in the options menu that is shown when the user
 * presses the MENU button.
 * 
 * The code for this class was adapted from the ZXing project:
 * http://code.google.com/p/zxing
 */
public class PreferencesActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {

	public static final String KEY_ONLINE_MODE = "preference_online";
	public static final String KEY_SOURCE_LANGUAGE_PREFERENCE = "sourceLanguageCodeOcrPref";
	public static final String KEY_TARGET_LANGUAGE_PREFERENCE = "targetLanguageCodeTranslationPref";
	public static final String KEY_TOGGLE_TRANSLATION = "preference_translation_toggle_translation";
	public static final String KEY_CONTINUOUS_PREVIEW = "preference_capture_continuous";
	public static final String KEY_PAGE_SEGMENTATION_MODE = "preference_page_segmentation_mode";
	public static final String KEY_OCR_TESSERACT_ONLY_MODE = "preference_tesseractonly";
	public static final String KEY_OCR_ENGINE_MODE = "preference_ocr_engine_mode";
	public static final String KEY_CHARACTER_BLACKLIST = "preference_character_blacklist";
	public static final String KEY_CHARACTER_WHITELIST = "preference_character_whitelist";
	public static final String KEY_TOGGLE_LIGHT = "preference_toggle_light";
	public static final String KEY_TRANSLATOR = "preference_translator";

	public static final String KEY_AUTO_FOCUS = "preferences_auto_focus";
	public static final String KEY_HELP_VERSION_SHOWN = "preferences_help_version_shown";
	public static final String KEY_NOT_OUR_RESULTS_SHOWN = "preferences_not_our_results_shown";
	public static final String KEY_REVERSE_IMAGE = "preferences_reverse_image";
	public static final String KEY_VIBRATE = "preferences_vibrate";

//	private CheckBoxPreference checkBoxOnlineMode;
//	private CheckBoxPreference checkBoxPreferenceContinuousPreview;
	private ListPreference listPreferenceSourceLanguage;
//	private CheckBoxPreference checkBoxPrePosProcessing;
//	private CheckBoxPreference checkBoxTranslate;
	private ListPreference listPreferenceTargetLanguage;
	private ListPreference listPreferenceOcrEngineMode;
	private EditTextPreference editTextPreferenceCharacterBlacklist;
	private EditTextPreference editTextPreferenceCharacterWhitelist;
	private ListPreference listPreferencePageSegmentationMode;
//	private CheckBoxPreference checkBoxReversedImage;

	private static SharedPreferences sharedPreferences;

	/**
	 * Set the default preference values.
	 * 
	 * @param Bundle
	 *            savedInstanceState the current Activity's state, as passed by
	 *            Android
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);

		sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

//		checkBoxOnlineMode = (CheckBoxPreference) getPreferenceScreen()
//				.findPreference(KEY_ONLINE_MODE);
//		checkBoxPreferenceContinuousPreview = (CheckBoxPreference) getPreferenceScreen()
//				.findPreference(KEY_CONTINUOUS_PREVIEW);
		listPreferenceSourceLanguage = (ListPreference) getPreferenceScreen()
				.findPreference(KEY_SOURCE_LANGUAGE_PREFERENCE);
//		checkBoxTranslate = (CheckBoxPreference) getPreferenceScreen()
//				.findPreference(KEY_TOGGLE_TRANSLATION);
		listPreferenceTargetLanguage = (ListPreference) getPreferenceScreen()
				.findPreference(KEY_TARGET_LANGUAGE_PREFERENCE);
//		checkBoxPrePosProcessing = (CheckBoxPreference) getPreferenceScreen()
//				.findPreference(KEY_OCR_TESSERACT_ONLY_MODE);
		listPreferenceOcrEngineMode = (ListPreference) getPreferenceScreen()
				.findPreference(KEY_OCR_ENGINE_MODE);
		editTextPreferenceCharacterBlacklist = (EditTextPreference) getPreferenceScreen()
				.findPreference(KEY_CHARACTER_BLACKLIST);
		editTextPreferenceCharacterWhitelist = (EditTextPreference) getPreferenceScreen()
				.findPreference(KEY_CHARACTER_WHITELIST);
		listPreferencePageSegmentationMode = (ListPreference) getPreferenceScreen()
				.findPreference(KEY_PAGE_SEGMENTATION_MODE);
//		checkBoxReversedImage = (CheckBoxPreference) getPreferenceScreen()
//				.findPreference(KEY_REVERSE_IMAGE);

		// Create the entries/entryvalues for the translation target language
		// list.
		initTranslationTargetList();

	}

	/**
	 * Interface definition for a callback to be invoked when a shared
	 * preference is changed. Sets summary text for the app's preferences.
	 * Summary text values show the current settings for the values.
	 * 
	 * @param sharedPreferences
	 *            the Android.content.SharedPreferences that received the change
	 * @param key
	 *            the key of the preference that was changed, added, or removed
	 */
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		// Update preference summary values to show current preferences
		if (key.equals(KEY_SOURCE_LANGUAGE_PREFERENCE)) {
			// Set the summary text for the source language name
			listPreferenceSourceLanguage
					.setSummary(LanguageCodeHelper.getOcrLanguageName(
							getBaseContext(),
							sharedPreferences
									.getString(
											key,
											CaptureActivity.DEFAULT_SOURCE_LANGUAGE_CODE)));

			// Retrieve the character blacklist/whitelist for the new language
			String blacklist = OcrCharacterHelper.getBlacklist(
					sharedPreferences, listPreferenceSourceLanguage.getValue());
			String whitelist = OcrCharacterHelper.getWhitelist(
					sharedPreferences, listPreferenceSourceLanguage.getValue());

			// Save the character blacklist/whitelist to preferences
			sharedPreferences.edit()
					.putString(KEY_CHARACTER_BLACKLIST, blacklist).commit();
			sharedPreferences.edit()
					.putString(KEY_CHARACTER_WHITELIST, whitelist).commit();

			// Set the blacklist/whitelist summary text
			editTextPreferenceCharacterBlacklist.setSummary(blacklist);
			editTextPreferenceCharacterWhitelist.setSummary(whitelist);

		} else if (key.equals(KEY_TARGET_LANGUAGE_PREFERENCE)) {
			listPreferenceTargetLanguage
					.setSummary(LanguageCodeHelper.getTranslationLanguageName(
							this,
							sharedPreferences
									.getString(
											key,
											CaptureActivity.DEFAULT_TARGET_LANGUAGE_CODE)));
		} else if (key.equals(KEY_PAGE_SEGMENTATION_MODE)) {
			listPreferencePageSegmentationMode.setSummary(sharedPreferences
					.getString(key,
							CaptureActivity.DEFAULT_PAGE_SEGMENTATION_MODE));
		} else if (key.equals(KEY_OCR_ENGINE_MODE)) {
			listPreferenceOcrEngineMode.setSummary(sharedPreferences.getString(
					key, CaptureActivity.DEFAULT_OCR_ENGINE_MODE));
		} else if (key.equals(KEY_CHARACTER_BLACKLIST)) {

			// Save a separate, language-specific character blacklist for this
			// language
			OcrCharacterHelper.setBlacklist(sharedPreferences,
					listPreferenceSourceLanguage.getValue(),
					sharedPreferences.getString(key, OcrCharacterHelper
							.getDefaultBlacklist(listPreferenceSourceLanguage
									.getValue())));

			// Set the summary text
			editTextPreferenceCharacterBlacklist.setSummary(sharedPreferences
					.getString(key, OcrCharacterHelper
							.getDefaultBlacklist(listPreferenceSourceLanguage
									.getValue())));

		} else if (key.equals(KEY_CHARACTER_WHITELIST)) {

			// Save a separate, language-specific character blacklist for this
			// language
			OcrCharacterHelper.setWhitelist(sharedPreferences,
					listPreferenceSourceLanguage.getValue(),
					sharedPreferences.getString(key, OcrCharacterHelper
							.getDefaultWhitelist(listPreferenceSourceLanguage
									.getValue())));

			// Set the summary text
			editTextPreferenceCharacterWhitelist.setSummary(sharedPreferences
					.getString(key, OcrCharacterHelper
							.getDefaultWhitelist(listPreferenceSourceLanguage
									.getValue())));

		}
	}

	/**
	 * Sets the list of available languages and the current target language for
	 * translation. Called when the key for the current translator is changed.
	 */
	void initTranslationTargetList() {
		// Set the preference for the target language code, in case we've just
		// switched from Google
		// to Bing, or Bing to Google.
		String currentLanguageCode = sharedPreferences.getString(
				KEY_TARGET_LANGUAGE_PREFERENCE,
				CaptureActivity.DEFAULT_TARGET_LANGUAGE_CODE);

		// Get the name of our language
		String currentLanguage = LanguageCodeHelper.getTranslationLanguageName(
				getBaseContext(), currentLanguageCode);
		String newLanguageCode = "";
		// Update the list of available languages for the currently-chosen
		// translation API.
		listPreferenceTargetLanguage
				.setEntries(R.array.translationtargetlanguagenames_microsoft);
		listPreferenceTargetLanguage
				.setEntryValues(R.array.translationtargetiso6391_microsoft);

		// Get the corresponding code for our language name
		newLanguageCode = TranslateBingAsyncTask.toLanguage(currentLanguage);

		// Store the code as the target language preference
		String newLanguageName = LanguageCodeHelper.getTranslationLanguageName(
				getBaseContext(), newLanguageCode);
		listPreferenceTargetLanguage.setValue(newLanguageName); // Set the radio
																// button in the
																// list
		sharedPreferences
				.edit()
				.putString(PreferencesActivity.KEY_TARGET_LANGUAGE_PREFERENCE,
						newLanguageCode).commit();
		listPreferenceTargetLanguage.setSummary(newLanguageName);
	}

	/**
	 * Sets up initial preference summary text values and registers the
	 * OnSharedPreferenceChangeListener.
	 */
	@Override
	protected void onResume() {
		super.onResume();
		// Set up the initial summary values
		listPreferenceSourceLanguage.setSummary(LanguageCodeHelper
				.getOcrLanguageName(getBaseContext(), sharedPreferences
						.getString(KEY_SOURCE_LANGUAGE_PREFERENCE,
								CaptureActivity.DEFAULT_SOURCE_LANGUAGE_CODE)));
		listPreferenceTargetLanguage.setSummary(LanguageCodeHelper
				.getTranslationLanguageName(getBaseContext(), sharedPreferences
						.getString(KEY_TARGET_LANGUAGE_PREFERENCE,
								CaptureActivity.DEFAULT_TARGET_LANGUAGE_CODE)));
		listPreferencePageSegmentationMode.setSummary(sharedPreferences
				.getString(KEY_PAGE_SEGMENTATION_MODE,
						CaptureActivity.DEFAULT_PAGE_SEGMENTATION_MODE));
		listPreferenceOcrEngineMode.setSummary(sharedPreferences.getString(
				KEY_OCR_ENGINE_MODE, CaptureActivity.DEFAULT_OCR_ENGINE_MODE));
		editTextPreferenceCharacterBlacklist.setSummary(sharedPreferences
				.getString(KEY_CHARACTER_BLACKLIST, OcrCharacterHelper
						.getDefaultBlacklist(listPreferenceSourceLanguage
								.getValue())));
		editTextPreferenceCharacterWhitelist.setSummary(sharedPreferences
				.getString(KEY_CHARACTER_WHITELIST, OcrCharacterHelper
						.getDefaultWhitelist(listPreferenceSourceLanguage
								.getValue())));

		// Set up a listener whenever a key changes
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}

	/**
	 * Called when Activity is about to lose focus. Unregisters the
	 * OnSharedPreferenceChangeListener.
	 */
	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);
	}
}